package com.prestamomo.vil.ppa.pmvilnet.pmvilutils;


import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class PmvilAnalyze {
    private static String pmvilCharset = "utf-8";
    private static int pmvilOffset = 16;
    private static String transPmvilformation = "AES/CBC/PKCS5Padding";
    private static String algorPmvilithm = "AES";

    //请求参数加密key:
    public static final String Pmvil_THICKEN_CANSHU = "emJvNFFGZmJNcHNrV054Vw==";
    //请求头加密key:
    public static final String Pmvil_THICKEN_HEADER = "f5Cw6wpQKjHkhyUzukH3444bG7xYnvoq";
    //请求参数加密名字：
    public static final String Pmvil_CANSHU_NAME = "DUNHDNGL";
    //请求头参数名字：
    public static final String Pmvil_HEADER_NAME = "IFORGJYES";
    private static final String headerPmvilKey = Pmvil_THICKEN_HEADER;
    private static final String decryptPmvilKey = Pmvil_THICKEN_CANSHU;

    public static String analyzePmvilCopy(String formPmvilStr, boolean isHeaderPmvilAnaly){
        return isHeaderPmvilAnaly? analyzePmvilText(formPmvilStr, headerPmvilKey.getBytes()) : analyzePmvilText(formPmvilStr, PmvilBs64Anal.decodePmvilBase64(decryptPmvilKey));
    }

    private static String analyzePmvilText(String formPmvilStr, byte[] keyToPmvilByte){
        SecretKeySpec secretPmvilKeySpec = new SecretKeySpec(keyToPmvilByte, transPmvilformation);
        IvParameterSpec ivParameterPmvilSpec = new IvParameterSpec(keyToPmvilByte, 0, pmvilOffset);
        return getPmvilStr(formPmvilStr, secretPmvilKeySpec, ivParameterPmvilSpec);
    }

    private static String getPmvilStr(String formPmvilStr, SecretKeySpec secretPmvilKeySpec, IvParameterSpec ivParameterPmvilSpec) {
        try {
            byte[] pmvilBytes = formPmvilStr.getBytes(pmvilCharset);
            Cipher instPmavilance = Cipher.getInstance(transPmvilformation);
            instPmavilance.init(Cipher.ENCRYPT_MODE, secretPmvilKeySpec, ivParameterPmvilSpec);
            return PmvilBs64Anal.pmvilEncodeBase64Str(instPmavilance.doFinal(pmvilBytes));
        } catch (Exception e) {
            return formPmvilStr;
        }
    }

    public static String decryptPmvilCopy(String nedDecryptPmvilStr){
        SecretKeySpec secretPmvilKeySpec = new SecretKeySpec( PmvilBs64Anal.decodePmvilBase64(decryptPmvilKey), algorPmvilithm);
        IvParameterSpec ivParamePmvilterSpec = new IvParameterSpec( PmvilBs64Anal.decodePmvilBase64(decryptPmvilKey), 0, pmvilOffset);
        try {
            Cipher pmvilCipher = Cipher.getInstance(transPmvilformation);
            pmvilCipher.init(Cipher.DECRYPT_MODE, secretPmvilKeySpec, ivParamePmvilterSpec);
            return new String( pmvilCipher.doFinal(PmvilBs64Anal.decodePmvilBase64(nedDecryptPmvilStr)));
        } catch (Exception e) {
            return nedDecryptPmvilStr;
        }
    }

}
